package dao;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import model.Candidate;
import model.User;

public class CandidateDAO {

	public CandidateDAO() {

	}

	public void addCandidate(Candidate candidate) {
		try {
			Connection connection = DBConnection.getDefaultConnection();
			Statement statement = connection.createStatement();

			String query = "INSERT INTO candidate (name) VALUES ('"
					+ candidate.getName() + "');";
			// System.out.println(query);
			statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);

			ResultSet keys = statement.getGeneratedKeys();
			keys.next();
			candidate.setId(keys.getInt(1));

			keys.close();
			statement.close();
			DBConnection.closeConnection(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void removeCandidate(Candidate candidate) {
		try {
			Connection connection = DBConnection.getDefaultConnection();
			Statement statement = connection.createStatement();

			String query = "DELETE FROM candidate WHERE candidate_id = "
					+ candidate.getId() + ";";
			System.out.println(query);
			statement.executeUpdate(query);
			statement.close();
			DBConnection.closeConnection(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("finally")
	public Candidate getCandidateById(int id) {
		Candidate candidate = null;
		try {
			Connection connection = DBConnection.getDefaultConnection();
			Statement statement = connection.createStatement();
			String query = "SELECT * FROM candidate WHERE candidate_id = " + id;
			// System.out.println(query);
			ResultSet rs = statement.executeQuery(query);
			candidate = loadCandidate(rs);
			statement.close();
			DBConnection.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return candidate;
		}
	}

	@SuppressWarnings("finally")
	public Candidate loadCandidate(ResultSet rs) {
		Candidate candidate = new Candidate();
		try {
			if (rs.next()) {
				candidate.setId(rs.getInt("candidate_id"));
				candidate.setName(rs.getString("name"));
			} else {
				return null;
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return candidate;
		}
	}

	@SuppressWarnings("finally")
	public List<Candidate> getAllCandidates() {
		ArrayList<Candidate> candidates = new ArrayList<Candidate>();
		try {
			Connection connection = DBConnection.getDefaultConnection();
			Statement statement = connection.createStatement();

			String query = "SELECT * FROM candidate";

			ResultSet rs = statement.executeQuery(query);

			while (rs.next()) {
				Candidate candidate = new Candidate();
				candidate.setId(rs.getInt("candidate_id"));
				candidate.setName(rs.getString("name"));
				candidate.setBigIntCode(BigInteger.valueOf(rs
						.getLong("bigIntCode")));
				candidates.add(candidate);
			}
			rs.close();
			statement.close();
			DBConnection.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return candidates;
		}
	}

	public void commitCandidate(Candidate candidate) {
		try {
			Connection connection = DBConnection.getDefaultConnection();
			Statement statement = connection.createStatement();

			String query = "UPDATE candidate SET name = '"
					+ candidate.getName()
					+ (candidate.getBigIntCode() != null ? "', bigIntCode = '"
							+ candidate.getBigIntCode() + "'" : "")
					+ " WHERE candidate_id = '" + candidate.getId() + "';";
			statement.executeUpdate(query);
			statement.close();
			DBConnection.closeConnection(connection);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// return activationCode;
		}
	}
}
